//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Common.Configuration.Design.Editors;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents a query using an Xml data file.
	/// </summary>
	public sealed class XmlFileQueryNode : FileQueryNode
	{
		#region Fields
		private string element;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the target element.
		/// </summary>
		/// <value>The target element for this node.</value>
		[Required]
		[QueryCategory("CategoryProvider")]
		[QueryDescription("XmlFileQueryElementDescription")]
		public string Element
		{
			get { return element; }
			set
			{
				if (value != null)
					element = value;
			}
		}

		/// <summary>
		/// Gets or sets the xml data file.
		/// </summary>
		/// <value>The xml data file for this node.</value>
		[Required]
		[Editor(typeof(FilteredFileNameEditor), typeof(UITypeEditor))]
		[FileFilter("XmlFileNameFilter")]
		public override string DataFile
		{
			get { return base.DataFile; }
			set
			{
				if (value != null)
					base.DataFile = value;
			}
		}

		/// <summary>
		/// Gets a <see cref="XmlFileQueryData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="XmlFileQueryData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override QueryDataBase QueryData
		{
			get { return new XmlFileQueryData(Name, CustomCommandName, ParameterSetName, DataFile, element); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="XmlFileQueryNode"/> class.
		/// </summary>
		public XmlFileQueryNode ()
			: this(new XmlFileQueryData(InternalResources.DefaultXmlFileQueryNodeName, string.Empty, string.Empty, string.Empty, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the<see cref="XmlFileQueryNode"/> class with a <see cref="XmlFileQueryData"/> configuration object.
		/// </summary>
		/// <param name="xmlFileQueryData">The XML file query data.</param>
		public XmlFileQueryNode (XmlFileQueryData xmlFileQueryData)
		{
			if (xmlFileQueryData == null)
				throw new ArgumentNullException("xmlFileQueryData");

			Rename(xmlFileQueryData.Name);
			base.CustomCommandName = xmlFileQueryData.CustomCommandName;
			base.ParameterSetName = xmlFileQueryData.ParameterSetName;
			base.DataFile = xmlFileQueryData.DataFile;
			this.element = xmlFileQueryData.Element;
		}
		#endregion
	}
}